﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EnOceanReceiver;
using EnOcean;
using EnOceanTransmitter;
using EnOcean.Radio.Sensor;


namespace TestClient
{
    public class Program
    {
        private static Receiver r;
        private static Transmitter t;

        /// <summary>
        /// Application starts here.
        /// </summary>
        public static void Main()
        {
            ConsoleKeyInfo key;
            long baseId = 0xFFCC6D00;

            r = new Receiver(null);
            r.TelegramReceived += new EventHandler<TelegramReceivedEventArgs>(TelegramReceived);
            r.Start();

            t = new Transmitter();
            t.TransmitterAvailable += new EventHandler(Transmitter_TransmitterAvailabe);
            t.Start();

            Console.WriteLine("Press q to exit.");
            while ((key = Console.ReadKey(true)).KeyChar != 'q')
            {
                switch (key.KeyChar)
                {
                    case '1':
                        Console.WriteLine("First - On");
                        t.Send(
                          new RepeatedSwitchNTelegram()
                          {
                              IsEnergyBowPressed = true,
                              FirstButtonId = 0,
                              FirstButtonState = ButtonState.I,
                              HasSecondAction = false,
                              SwitchType = SwitchType.Type2,
                              Id = baseId + 2
                          });
                        break;

                    case '2':
                        Console.WriteLine("Second - Off");
                        t.Send(
                          new RepeatedSwitchNTelegram()
                          {
                              IsEnergyBowPressed = true,
                              FirstButtonId = 0,
                              FirstButtonState = ButtonState.O,
                              HasSecondAction = false,
                              SwitchType = SwitchType.Type2,
                              Id = baseId + 2
                          });
                        break;

                    case '3':
                        Console.WriteLine("Release");
                        t.Send(
                          new RepeatedSwitchUTelegram()
                          {
                              IsEnergyBowPressed = false,
                              SwitchType = SwitchType.Type2,
                              Buttons = 0,
                              Id = baseId + 2
                          });
                        break;

                    case '4':
                        Console.WriteLine("Central command - Dimming");
                        t.Send(
                                new FourByteSensorDataTelegram()
                                {
                                    Id = baseId + 2,
                                    Data3 = 0x02,
                                    Data2 = 0x20,
                                    Data1 = 0x00,
                                    Data0 = 0x01
                                }
                            );
                        break;
                }
            }

            r.Stop();
            t.Stop();
        }

        static void Transmitter_TransmitterAvailabe(object sender, EventArgs e)
        {

            Console.WriteLine("Transmitter_TransmitterAvailabe");

            //RepeatedSwitchNTelegram s;

            //s = new RepeatedSwitchNTelegram()
            //{
            //  IsEnergyBowPressed = true,
            //  FirstButtonId = 0,
            //  FirstButtonState = ButtonState.O,
            //  HasSecondAction = false,
            //  SwitchType = SwitchType.Type2,
            //  Id = 0xFFCC6D02
            //};

            //Console.WriteLine("Transmitter available");
            //foreach (long id in t.GetAvailableBaseIds())
            //{
            //  Console.WriteLine(id.ToString("X8"));

            //  if (id == 0xFFCC6D00)
            //  {
            //    Console.WriteLine("Sending telegram");
            //    t.Send(s);  
            //  }
            //}
        }

        static void TelegramReceived(object sender, TelegramReceivedEventArgs e)
        {
            Console.WriteLine(e.Telegram.ToString());
        }
    }
}
